<?php

namespace app\api\controller;

use app\common\model\Attachment as AttachmentModel;
use app\model\Supply as SupplyModel;
use think\Db;
use think\facade\Log;
use think\facade\Request;

/**
 * @title   供求接口
 * @desc    供求接口
 * Class Standard
 * @package app\api\controller
 */

class Supply extends Common
{
    /**
     * @title 供求列表
     * @desc  供求列表
     *
     * @param int $title
     * @param int $limit 10
     *
     * @return Array $list 供求列表
     */
    public function lists()
    {
        // try {
        //     $msg = $this->Auth();
        //     if (!$msg) {
        $title = $this->request->param('title');
        $uid = $this->request->param('uid');
        $class = $this->request->param('class');
        $typeid = $this->request->param('typeid');
        $places = $this->request->param('places');
        $limit = $this->request->param('limit');

        if ($title != '') {
            $map[] = ['title', 'like', '%' . $title . '%'];
        }
        if ($class != '') {
            //供求分类
            $map[] = ['class', '=', $class];
        }
        if ($typeid != '') {
            //产品类别ID
            $map[] = ['typeid', '=', $typeid];
        }
        if ($places != '') {
            //推荐位，是否显示在首页或指定位置
            $map[] = ['places', '=', $places];
        }
        if ($uid != '') {
            $map[] = ['uid', '=', $uid];
        }
        if (!$limit) {
            $limit = 10;
        }
        //dump(SupplyModel::where($map)->attachment->path);
        // $list = SupplyModel::get(1);
        $list = SupplyModel::where($map)
            ->field(['id', 'title', 'pictures'])
            ->order('id desc')
            ->paginate($limit);

        foreach ($list as $gq) {
            $Attachment = AttachmentModel::get($gq['pictures']);
            $gq['pictures'] = $this->getPicPath($Attachment['path']);
        }
        return $this->jsonPage($list->total(), $list->toArray()['data']);
    }

    /**
     * 产品内容
     * @return string
     */
    public function info()
    {
        // if ($this->request->isPost()) {
        //     $msg = $this->Auth();
        //     if (!$msg) {
        $id = $this->request->param('id');
        // $data = SupplyModel::get($id);
        $data = SupplyModel::where('id', $id)
            ->field(['title', 'pictures', 'supplyinfo', 'mid'])
            ->append(['company', 'nickname', 'telephone'])
            ->select();

        foreach ($data as $gq) {
            $Attachment = AttachmentModel::get($gq['pictures']);
            $gq['pictures'] = $this->getPicPath($Attachment['path']);
            $gq['company'] = $this->getMember($gq['mid'])->company;
            $gq['nickname'] = $this->getMember($gq['mid'])->nickname;
            $gq['telephone'] = $this->getMember($gq['mid'])->telephone;
        }
        // if (!empty($data->content->content)) {
        //     $data->content->content = html_entity_decode($data->content->content);
        // }

        return $this->jsonSuccess('成功', $data);
        //     } else {
        //         return $msg;
        //     }
        // } else {
        //     return $this->jsonStop();
        // }
    }
}
